﻿
; (function (window, undefined, $) {

    var submiting = false;
    $('#frmEdit').validator({
        rules: {

        },
        fields: {
            '#CompanyName': 'required',
            '#Adress': 'required',
            '#Employees': 'required',
            '#Industry': 'required',
            '#Nature': 'required',
            '#Synopsis': 'required',
            '#Constitution': 'required'
        },
        valid: function (form) {
            if (submiting) {
                return;
            }
            submiting = true;
            FormSubmit(form, function (res) {
                if (res.Status > 0) {
                    alert(res.Message);
                    window.location.href = "../Company/Index";
                } else {
                    alert(res.Message);
                }
                submiting = false;
            })
        }
    });
    $("#btnSave").click(function (e) {
        $("#frmEdit").submit();
    });

    $("#btnReturn").click(function (e) {

        window.location.href = "/Admin/Company/Index";
    });

    init();

    //初始化  

    function init() {
        //初始化图片上传  
        var btnImg1 = document.getElementById("btnUploadImg1");
        var fileName1 = document.getElementById("fileName1");
        var img1 = document.getElementById("imgShow1");

        var btnImg2 = document.getElementById("btnUploadImg2");
        var fileName2 = document.getElementById("fileName2");
        var img2 = document.getElementById("imgShow2");

        var btnImg3 = document.getElementById("btnUploadImg3");
        var fileName3 = document.getElementById("fileName3");
        var img3 = document.getElementById("imgShow3");

        var btnImg4 = document.getElementById("btnUploadImg4");
        var fileName4 = document.getElementById("fileName4");
        var img4 = document.getElementById("imgShow4");


        var TitlePic = "111";
        var Pictures = "111";


        g_AjxUploadImg(btnImg1, img1, TitlePic, Pictures, fileName1);
        g_AjxUploadImg(btnImg2, img2, TitlePic, Pictures, fileName2);
        g_AjxUploadImg(btnImg3, img3, TitlePic, Pictures, fileName3);
        g_AjxUploadImg(btnImg4, img4, TitlePic, Pictures, fileName4);

    }

    var g_AjxTempDir = "/FileUpload/";

    //图片上传  

    function g_AjxUploadImg(btn, img, TitlePic, Pictures, fileName) {
        var button = btn, interval;
        new AjaxUpload(button, {
            action: options.uploadUrl,
            data: {},
            name: 'myfile',
            onSubmit: function (file, ext) {
                if (!(ext && /^(jpg|JPG|png|PNG|gif|GIF)$/.test(ext))) {
                    alert("您上传的图片格式不对，请重新选择！");
                    return false;
                }
            },
            onComplete: function (file, response) {
                flagValue = response;
                if (flagValue == "-1") {
                    alert("您上传的图片格式不对，请重新选择！");
                } else if (flagValue == "-2") {
                    alert("您上传的图片大于200K，请重新选择！");
                } else if (flagValue == "-3") {
                    alert("图片上传失败！");
                } else {
                    alert("上传成功！");
                    var strs = new Array();
                    strs = response.split("/");

                    var Pictures2 = "";
                    var fileName2 = "";
                    for (var i = 0; i < strs.length; i++) {
                        if (i == 0) {
                            TitlePic.value = strs[i];
                        } else if (i == 1) {
                            Pictures.value = strs[i];
                            Pictures2 = strs[i];

                        } else if (i == 2) {
                            fileName2 = strs[i];
                        }
                    }
                    img.src = g_AjxTempDir + fileName2 + "/" + Pictures2;
                    fileName.value = g_AjxTempDir + fileName2 + "/" + Pictures2;
                }
            }
        });
    }

})(window, undefined, jQuery);